package cn.wolfcode.orderserver.service.impl;

import cn.wolfcode.orderapi.domain.Order;
import cn.wolfcode.orderserver.service.IOrderService;
import cn.wolfcode.productapi.domain.Product;
import cn.wolfcode.productapi.feign.IProductFeignApi;
import com.netflix.discovery.converters.Auto;
import com.sun.org.apache.xpath.internal.SourceTree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;
import java.util.UUID;

@Service
public class OrderServiceImpl implements IOrderService {
//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private IProductFeignApi productFeignApi;

    @Override
    public Order save(Long userId, Long productId) {

        //Product product = restTemplate.getForObject("http://localhost:8889/get?id=" + productId, Product.class);
        // ribbon 负载均衡
        //Product product = restTemplate.getForObject("http://PRODUCT-SERVER/get?id=" + productId, Product.class);

        //使用 feign 负载均衡
        Product product = productFeignApi.get(productId);
        System.out.println(productFeignApi.getClass());
        Order order = new Order();
        order.setOrderNo(UUID.randomUUID().toString().replace("-","" ));
        order.setCreateTime(new Date());
        order.setUserId(userId);
        order.setProductName(product.getName());
        order.setProductPrice(product.getPrice());

        System.out.println("执行订单操作");
        return order;
    }
}
